<?php
/**
 * 根据不同路径返回无数据绑定的不同名 view
 * User: zjx
 * Date: 2016/10/24
 * Time: 17:10
 */

namespace App\Http\Controllers;

use App\Http\Controllers\WebPage\Seo\ProductionController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;

class WebPageController extends Controller
{
    /**
     * 如果路径是 a/b/c 请把 view 放到 a/b/c.blade.php ,
     * 如果路径与view文件目录不对应，需要在这个数组中声明路径与 view名字的关系
     *
     * @var array
     */
    protected $path_view_map = [
        '/' => 'welcome' ,
        
        /**
         * @deprecated
         * @see ProductionController::index()
         */
//        'production' => 'official_website.production' ,
    ];

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function view(Request $request)
    {
        if (isset($this->path_view_map[$request->path()])) {
            return view($this->path_view_map[$request->path()]);
        }

        $view = str_replace('/','.', $request->path());
        if (View::exists($view)) {
            return view($view);
        }

        return $this->{$request->path()};
    }

    /**
     * @param string $view
     * @param array $parameters
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function __call($view, $parameters)
    {
        if (View::exists($view)) return view($view);

        return abort(404);
    }
}